{% extends 'web/base.html' %}
{% load staticfiles %}
{% load my_filters %}
{% block title %}
    企小宝-商品-编辑
{% endblock %}
{% block content %}
    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form" id="ProductInfo">
                {% csrf_token %}
                <input type="hidden" name="id" value="{{ product.id }}">
                <div class="layui-form-item">
                    <label for="title" class="layui-form-label">
                        <span class="x-red">*</span>标题</label>
                    <div class="layui-input-inline col-md-6">
                        <input id="title" name="title" value="{{ product.title }}" class="layui-input"></div>
                </div>
                <div class="layui-form-item">
                    <label for="title" class="layui-form-label">
                        <span class="x-red">*</span>规格（/）</label>
                    <div class="layui-input-inline col-md-6">
                        <input id="specs" name="specs" value="{{ product.specs }}" class="layui-input"></div>
                </div>
                <div class="layui-form-item">
                    <label for="title" class="layui-form-label">
                        <span class="x-red">*</span>商品图</label>
                    <div class="layui-input-inline">
                        <div class="layui-upload">
                            <button type="button" class="layui-btn" id="image_btn">上传图片</button>
                            <div class="layui-upload-list">
                                <img class="layui-upload-img" src="{{ product.get_image_url }}" id="image_preview"
                                     style="width: 250px;height: 200px;">
                                <input type="hidden" name="image" id="image" value="{{ product.image }}">
                                <p id="demoText"></p>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="price_original" class="layui-form-label">
                        <span class="x-red">*</span>原价(元)</label>
                    <div class="layui-input-inline">
                        <input type="text" id="price_original" value="{{ product.price_original|div100 }}"
                               name="price_original" lay-verify="required|number"
                               autocomplete="off" class="layui-input" placeholder="保留2位小数"></div>
                    <label for="price_discount" class="layui-form-label">
                        <span class="x-red">*</span>底价(元)</label>
                    <div class="layui-input-inline">
                        <input type="text" id="price_discount" value="{{ product.price_discount|div100 }}"
                               name="price_discount" lay-verify="required|number"
                               autocomplete="off" class="layui-input" placeholder="保留2位小数"></div>
                </div>
                <div class="layui-form-item">
                    <label for="price_original" class="layui-form-label">库存</label>
                    <div class="layui-input-inline">
                        <input type="number" id="inventory" name="inventory" lay-verify="number"
                               autocomplete="off" class="layui-input" placeholder="不填表示不限制"
                        value="{{ product.inventory }}">
                    </div>
                    <label for="price_discount" class="layui-form-label">优先级</label>
                    <div class="layui-input-inline">
                        <input type="number" id="priority" name="priority" lay-verify="number"
                               autocomplete="off" class="layui-input" placeholder="数字越大优先级越高"
                        value="{{ product.priority }}">
                    </div>
                </div>

                <div class="layui-row">
                    <label for="desc" class="layui-form-label">
                        <span class="x-red">*</span>商品介绍</label>
                    <div class="layui-input-inline ">
                        <textarea id="desc" name="desc" style="display: none;">{{ product.desc }}</textarea></div>
                </div>
                <div class="layui-form-item">
                    <label for="L_repass" class="layui-form-label"></label>
                    <button class="layui-btn" lay-filter="add" lay-submit="">保存并预览</button>
                </div>
            </form>
        </div>
    </div>
    <script>
        layui.use(['form', 'layer', 'laydate', 'upload', 'layedit', 'jquery'],
            function () {
                $ = layui.jquery;
                var form = layui.form,
                    layer = layui.layer,
                    laydate = layui.laydate,
                    upload = layui.upload,
                    layedit = layui.layedit;
                ;
                upload.config.data = {
                    'csrfmiddlewaretoken': $.cookie("csrftoken")
                }
                upload.config.url = '{% url "common:upload" %}';

                //自定义验证规则
                form.verify({
                    nikename: function (value) {
                        if (value.length < 5) {
                            return '昵称至少得5个字符啊';
                        }
                    },
                    pass: [/(.+){6,12}$/, '密码必须6到12位'],
                    repass: function (value) {
                        if ($('#L_pass').val() != $('#L_repass').val()) {
                            return '两次密码不一致';
                        }
                    }
                });
                //图片上传
                var uploadInst = upload.render({
                    elem: '#image_btn'
                    , before: function (obj) {
                        //预读本地文件示例，不支持ie8
                        obj.preview(function (index, file, result) {
                            $('#image_preview').attr('src', result); //图片链接（base64）
                        });
                    }
                    , done: function (res) {
                        //如果上传失败
                        console.log(res)
                        if (res.code > 0) {
                            return layer.msg('上传失败');
                        }
                        //上传成功
                        $('#image').val(res.name)
                    }
                    , error: function () {
                        //演示失败状态，并实现重传
                        var demoText = $('#demoText');
                        demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                        demoText.find('.demo-reload').on('click', function () {
                            uploadInst.upload();
                        });
                    }
                });
                //富文本编辑器
                layedit.set({
                    uploadImage: {
                        url: '{% url "common:upload" %}' //接口url
                        , type: 'post' //默认post
                        , tdata: {
                            'csrfmiddlewaretoken': $.cookie("csrftoken")
                        }
                    }
                });
                var desc_t = layedit.build('desc', {
                    height: 180,//设置编辑器高度,
                    tool: ['strong', 'italic', 'underline', 'del', 'addhr',
                        '|',//分割线
                        'fontFomatt', 'colorpicker', 'left', 'center', 'right',
                        '|',
                        'link', 'unlink', 'image', 'anchors', 'face', 'fullScreen'
                    ]
                });
                //监听提交
                form.on('submit(add)',
                    function (data) {
                        $('#desc').val(layedit.getContent(desc_t));
                        debugger;
                        $.post("{% url 'product:edit' shop.id %}", $(data.form).serialize(), function (res) {
                                debugger;
                                console.log(res);
                                if (res['status'] == 0) {
                                    layer.alert(res.detail, {
                                            icon: 6
                                        },
                                        function () {
                                            xadmin.father_reload();
                                        });
                                } else {
                                    alert(res['detail'])
                                }
                            }
                        );
                        return false;
                    });
            })
        ;</script>
{% endblock %}